// pages/shop_order/order_search/order_search.js
const api = require('../../../utils/util.js');
const service = require('../../../utils/service.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    shopData: '',
    orderList: [],
    page: 1,
    shopId: '',
    tem_order_id: '',
    refundPrice: '',
    order_return_id: '',
    searchName: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      searchName: options.searchName,
      shopData: wx.getStorageSync('shop_data')
    })
    let scrolltobottom = 0;
    this.orderListGet(scrolltobottom);
  },

  orderListGet: function (scrolltobottom) {
    var that = this;

    api.ajax(service.url('orderListGet'), {
      unionid: this.data.shopData.shop_unionid,
      token: this.data.shopData.token,
      token_key: this.data.shopData.token_key,
      page: this.data.page,
      search: this.data.searchName
    },
      function (res) {
        if (res.code) {
          api.toast(res.message)
        } else {
          if (res.data != '') {
            console.log(res.data);
            if (!scrolltobottom) {
              that.data.orderList = [];
            }
            that.data.orderList = that.data.orderList.concat(res.data.list);

            if (res.data.list.length > 0) {
              that.setData({
                orderList: that.data.orderList,
                showListTag: true
              })
            } else {
              if (!scrolltobottom) {
                that.setData({
                  showListTag: false
                })
                api.toast('该状态下没有任何订单');
              } else {
                if (that.page != 1) {
                  that.setData({
                    page: parseInt(that.data.page - 1)
                  })
                  api.toast('没有更多数据...');
                }
              }
            }
            console.log("获取商品列表成功!");
          } else if (res.data == '') {
            that.orderList = [];
            api.toast({
              msg: '该状态下没有任何订单',
              duration: 2000,
              location: 'middle'
            });
          }
        }
      });
  },

  //搜索事件
  openSearch: function (e) {
    this.setData({
      searchName: e.detail.value
    })
    var scrolltobottom = 0;
    this.orderListGet(scrolltobottom);
  },

  //--底下按钮点击
  orderModBtn: function (e) {
    var that = this;
    let mod = e.currentTarget.dataset.mod;
    let order_id = e.currentTarget.dataset.orderId;

    switch (mod) {
      case "shopChat":
        wx.showModal({
          title: '提示',
          content: '下载APP更方便联系买家，是否前往下载?',
          success: function (rel) {
            if (rel.confirm) {
              wx.navigateTo({
                url: '../download_app/download_app',
              })
            }
          }
        })
        break;
      case "shopSend":
        //--发货
        wx.navigateTo({
          url: '../shop_send/send?order_id=' + order_id,
        })
        break;
      case "shopDel":
        //--确认是否删除订单
        wx.showModal({
          title: '提示',
          content: '是否删除订单?',
          success: function (rel) {
            if (rel.confirm) {
              api.ajax(service.url('sellerorderDelOrder'), {
                unionid: that.data.shopData.shop_unionid,
                token: that.data.shopData.token,
                token_key: that.data.shopData.token_key,
                order_id: order_id
              },
                function (res) {
                  if (res.code) {
                    api.toast(res.message);
                  } else {
                    var scrolltobottom = 0;
                    that.orderListGet(scrolltobottom);

                    api.toast('订单已删除');
                  }
                });
            }
          }
        });
        break;
      case "shopFlow":
        //--查看物流
        wx.navigateTo({
          url: '../shop_flow/flow?order_id=' + order_id,
        })
        break;
      case "shopRefundRefuse":
        //--商家拒绝退款
        wx.navigateTo({
          url: '../refuse_refun/refuse_refun?order_id=' + order_id,
        })
        break;
      case "shopRefundPass":
        // 同意申请
        this.getOrderDetailInfo(order_id, function (res) {
          console.log(res);

          wx.showModal({
            title: '提示',
            content: '是否同意退货退款申请?',
            success: function (rel) {
              if (rel.confirm) {
                api.ajax(service.url('agreeReturnOrder'), {
                  unionid: that.data.shopData.shop_unionid,
                  token: that.data.shopData.token,
                  token_key: that.data.shopData.token_key,
                  order_return_id: res.data.refund.order_return_id
                }, function (res) {
                  if (res.code == 0) {
                    var scrolltobottom = 0;
                    that.orderListGet(scrolltobottom);
                    api.toast('操作成功');

                  } else {
                    api.toast(res.message)
                  }
                })
              }
            }
          })
        });
        break;
      default:
        break;
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.setData({
      page: parseInt(this.data.page + 1)
    })
    var scrolltobottom = 1;
    this.orderListGet(scrolltobottom);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})